﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TCCVoigt.Models;

namespace TCCVoigt.Controllers
{
    public class RecursoAtitudeController : Controller
    {
        private TCCVoigtEntities db = new TCCVoigtEntities();

        //
        // GET: /RecursoAtitude/

        public ActionResult Index(int? id)
        {
            if (id.HasValue)
            {
                var recursoatitude = db.RecursoAtitude.Include("Atitude").Include("Recurso").Where(p => p.CodigoRecurso == id);
                return View(recursoatitude.ToList());
            }
            else
            {
                var recursoatitude = db.RecursoAtitude.Include("Atitude").Include("Recurso").OrderBy(p => p.Recurso.Descricao);
                return View(recursoatitude.ToList());
            }
        }

        //
        // GET: /RecursoAtitude/Details/5

        public ActionResult Details(int id = 0)
        {
            RecursoAtitude recursoatitude = db.RecursoAtitude.Single(r => r.Codigo == id);
            if (recursoatitude == null)
            {
                return HttpNotFound();
            }
            return View(recursoatitude);
        }

        //
        // GET: /RecursoAtitude/Create

        public ActionResult Create()
        {
            ViewBag.CodigoAtitude = new SelectList(db.Atitude.OrderBy(p => p.Descricao), "Codigo", "Descricao");
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao");
            return View();
        }

        //
        // POST: /RecursoAtitude/Create

        [HttpPost]
        public ActionResult Create(RecursoAtitude recursoatitude)
        {
            if (ModelState.IsValid)
            {
                db.RecursoAtitude.AddObject(recursoatitude);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CodigoAtitude = new SelectList(db.Atitude.OrderBy(p => p.Descricao), "Codigo", "Descricao", recursoatitude.CodigoAtitude);
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao", recursoatitude.CodigoRecurso);
            return View(recursoatitude);
        }

        //
        // GET: /RecursoAtitude/Edit/5

        public ActionResult Edit(int id = 0)
        {
            RecursoAtitude recursoatitude = db.RecursoAtitude.Single(r => r.Codigo == id);
            if (recursoatitude == null)
            {
                return HttpNotFound();
            }
            ViewBag.CodigoAtitude = new SelectList(db.Atitude.OrderBy(p => p.Descricao), "Codigo", "Descricao", recursoatitude.CodigoAtitude);
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao", recursoatitude.CodigoRecurso);
            return View(recursoatitude);
        }

        //
        // POST: /RecursoAtitude/Edit/5

        [HttpPost]
        public ActionResult Edit(RecursoAtitude recursoatitude)
        {
            if (ModelState.IsValid)
            {
                db.RecursoAtitude.Attach(recursoatitude);
                db.ObjectStateManager.ChangeObjectState(recursoatitude, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CodigoAtitude = new SelectList(db.Atitude.OrderBy(p => p.Descricao), "Codigo", "Descricao", recursoatitude.CodigoAtitude);
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao", recursoatitude.CodigoRecurso);
            return View(recursoatitude);
        }

        //
        // GET: /RecursoAtitude/Delete/5

        public ActionResult Delete(int id = 0)
        {
            RecursoAtitude recursoatitude = db.RecursoAtitude.Single(r => r.Codigo == id);
            if (recursoatitude == null)
            {
                return HttpNotFound();
            }
            return View(recursoatitude);
        }

        //
        // POST: /RecursoAtitude/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            RecursoAtitude recursoatitude = db.RecursoAtitude.Single(r => r.Codigo == id);
            db.RecursoAtitude.DeleteObject(recursoatitude);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}